Skip to content

Improve contravariance example in "Using Variance in Delegates" documentation #47052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 2, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 1, 2025

The existing contravariance example in the "Using Variance in Delegates" documentation was causing confusion for users who couldn't understand how the MultiHandler method could handle both KeyDown and MouseClick events.

Changes Made

1. Created Complete Working Example

  • Added a full console application in snippets/using-variance-in-delegates/ContravarianceExample.cs
  • Demonstrates contravariance with custom KeyEventArgs and MouseEventArgs classes
  • Shows how one MultiHandler method handles multiple event types
  • Includes executable code that clearly demonstrates the concept

2. Enhanced Documentation

  • Added "How contravariance works in this example" section with step-by-step explanation
  • Provided numbered steps showing the logical flow of contravariance
  • Explained why the compiler allows a method with EventArgs parameter to handle events expecting more specific types
  • Added practical demonstration output description

3. Improved Code Organization

  • Moved from inline code blocks to proper snippet references following documentation guidelines
  • Created dedicated project file for the example
  • Added comprehensive comments explaining the inheritance hierarchy and contravariance mechanics

Example Output

The new example produces clear output demonstrating the concept:

Demonstrating contravariance in event handlers:
Same MultiHandler method handles both events!

Simulating key press: Enter
MultiHandler called at: 18:08:30.172
  - Key event: Enter
Simulating mouse click at (100, 200)
MultiHandler called at: 18:08:30.180
  - Mouse event: (100, 200)

This directly addresses the user's confusion by showing exactly how contravariance enables one handler to work with multiple event types, making the documentation much clearer and more educational.

Fixes #18540.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
docs/csharp/programming-guide/concepts/covariance-contravariance/using-variance-in-delegates.md docs/csharp/programming-guide/concepts/covariance-contravariance/using-variance-in-delegates

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Not able to understand the example Improve contravariance example in "Using Variance in Delegates" documentation Jul 1, 2025
Copilot finished work on behalf of BillWagner July 1, 2025 18:09
@Copilot Copilot AI requested a review from BillWagner July 1, 2025 18:09
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great start. I have a few suggestions for improvement.

…lock syntax, remove unnecessary console output

Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
@Copilot Copilot AI requested a review from BillWagner July 2, 2025 13:30
Copilot finished work on behalf of BillWagner July 2, 2025 13:30
Co-authored-by: BillWagner <493969+BillWagner@users.noreply.github.com>
Copilot finished work on behalf of BillWagner July 2, 2025 13:39
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ready for final review.

@BillWagner BillWagner marked this pull request as ready for review July 2, 2025 13:43
@BillWagner BillWagner requested a review from a team as a code owner July 2, 2025 13:43
@BillWagner BillWagner requested a review from IEvangelist July 2, 2025 13:43
…rify custom types

Co-authored-by: IEvangelist <7679720+IEvangelist@users.noreply.github.com>
@Copilot Copilot AI requested a review from IEvangelist July 2, 2025 14:23
Copilot finished work on behalf of IEvangelist July 2, 2025 14:23
@BillWagner BillWagner merged commit 75198f1 into main Jul 2, 2025
10 checks passed
@BillWagner BillWagner deleted the copilot/fix-18540 branch July 2, 2025 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not able to understand the example
3 participants